﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WebScraperLibrary;

namespace WinForm
{
    public partial class Form1 : Form
    {
        private Button btn = new Button();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var culture = new System.Globalization.CultureInfo("sv-SE");
            var dayAfterTomorrow = culture.DateTimeFormat.GetDayName(DateTime.Today.AddDays(2).DayOfWeek);
            var treDagar = culture.DateTimeFormat.GetDayName(DateTime.Today.AddDays(3).DayOfWeek);
            var fyraDagar = culture.DateTimeFormat.GetDayName(DateTime.Today.AddDays(4).DayOfWeek);
            var femDagar = culture.DateTimeFormat.GetDayName(DateTime.Today.AddDays(5).DayOfWeek);
            var sexDagar = culture.DateTimeFormat.GetDayName(DateTime.Today.AddDays(6).DayOfWeek);

            dayAfterTomorrow = UppercaseFirst(dayAfterTomorrow);
            treDagar = UppercaseFirst(treDagar);
            fyraDagar = UppercaseFirst(fyraDagar);
            femDagar = UppercaseFirst(femDagar);
            sexDagar = UppercaseFirst(sexDagar);

            cmdÖvermorgon.Text = dayAfterTomorrow.ToString();
            cmdTreDagar.Text = treDagar;
            cmdFyraDagar.Text = fyraDagar;
            cmdFemDagar.Text = femDagar;
            cmdSexDagar.Text = sexDagar;

            var parser = new TvNuParser();
            var scraper = new WebScraper();

            var data = scraper.GetDataFromUrl("http://www.tv.nu");
            var channelList = parser.GetChannels(data);

            cbKanaler.DataSource = channelList;
            cbKanaler.DisplayMember = "Name";
            cbKanaler.ValueMember = "Link";

            ChannelSettings("");

            btn = cmdIdag;
            btn.ForeColor = Color.Red;


        }

        private void cmdButton_Click(object sender, EventArgs e)
        {
            btn.ForeColor = Color.Black;
            btn = (Button) sender;
            var link = ConvertButtonText(btn.Text);
            ChannelSettings("/" + link);
            btn.ForeColor = Color.Red;

        }

        private void cbKanaler_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChannelSettings("");
        }

        string UppercaseFirst(string s)
        {
            // Check for empty string.
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            // Return char and concat substring.
            return char.ToUpper(s[0]) + s.Substring(1);
        }

        private void dgwChannels_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            txtDescription.Text = dgwChannels.Rows[e.RowIndex].Cells[2].Value.ToString();


            if (dgwChannels.Rows[e.RowIndex].Cells[3].Value != null)
            {
                var src = dgwChannels.Rows[e.RowIndex].Cells[3].Value.ToString();
                pbDescription.Load("http:" + src);
                pbDescription.Size = pbDescription.Image.Size;
            }
            else
            {
                pbDescription.Image = null;
            }
        }

        private void ChannelSettings(string weekDay)
        {
            var parser = new TvNuParser();
            var scraper = new WebScraper();

            var channel = (Channel)cbKanaler.SelectedItem;

            var selectedData = scraper.GetDataFromUrl("http://www.tv.nu" + channel.Link + weekDay);
            selectedData = parser.GetProgramsData(selectedData);
            var dataList = parser.GetTimeAndShow(selectedData);

            dgwChannels.DataSource = dataList;

            if (dgwChannels.Columns[2] != null)
                dgwChannels.Columns[2].Visible = false;

            if (dgwChannels.Columns[3] != null)
                dgwChannels.Columns[3].Visible = false;

            dgwChannels.Update();
        }

        private string ConvertButtonText(string link)
        {
            link = link.Replace("ö", "o");
            link = link.Replace("å", "a");
            link = link.Replace("ä", "a");
            link = link.ToLower();

            return link;
        }

    }
}
